package com.halixun.d2app;


import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.halixun.d2app.base.BaseActivity;
import com.halixun.d2app.base.BaseResult;
import com.halixun.d2app.bean.CheckDeviceInfo;
import com.halixun.d2app.net.CreateRequestEntity;
import com.halixun.d2app.net.DataState;
import com.halixun.d2app.net.errer.ResultVerifier;
import com.halixun.d2app.ui.BoundActivity;
import com.halixun.d2app.ui.LoginActivity;
import com.halixun.d2app.utils.RequestDataUtils;
import com.halixun.d2app.utils.SystemUtil;
import com.halixun.d2app.utils.VersionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import pub.devrel.easypermissions.EasyPermissions;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WelcomeActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks{

    private Timer timer;
    private TimerTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        hideActionBar();
        DataState.getInstance().appEnter(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        timer = new Timer();
        task = new TimerTask() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void run() {
                check();
            }
        };
        String[] mPermissions = new String[]{Manifest.permission.READ_PHONE_STATE};
        if (EasyPermissions.hasPermissions(this, mPermissions)){
            timer.schedule(task, 1 * 1000);
        }else{
            EasyPermissions.requestPermissions(this,"请给予权限,否则app无法正常运行",998,mPermissions);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void check() {
        CreateRequestEntity.getWebService().checkDevice(RequestDataUtils.getRequestDataParam(this)).enqueue(new Callback<BaseResult<CheckDeviceInfo>>() {
            @Override
            public void onResponse(Call<BaseResult<CheckDeviceInfo>> call, Response<BaseResult<CheckDeviceInfo>> response) {
                if (ResultVerifier.isSucceed(response)) {
                    if (response.body().Data == null) {
                        toBound();
                    } else {
                        toSkipLogin(DataState.getInstance().isLoginState());
                    }
                } else {
                    ResultVerifier.serceErrers(response);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<BaseResult<CheckDeviceInfo>> call, Throwable t) {
                ResultVerifier.systerErrers(t);
                finish();
            }
        });
    }


    private void toSkipLogin(boolean loginState) {
        startActivity(new Intent(this, loginState ? MainActivity.class : LoginActivity.class));
        finish();
    }

    private void toBound() {
        startActivity(new Intent(this, BoundActivity.class));
        finish();
    }


    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        switch (requestCode){
            case 998:
                timer.schedule(task, 2 * 1000);
                break;
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        //处理权限名字字符串
        StringBuffer sb = new StringBuffer();
        for (String str : perms){
            sb.append(str);
            sb.append("\n");
        }
        sb.replace(sb.length() - 2,sb.length(),"");

        switch (requestCode){
            case 998:
                Toast.makeText(this, "已拒绝权限" + perms.get(0), Toast.LENGTH_SHORT).show();
                break;
        }
//        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
//            Toast.makeText(this, "已拒绝权限" + sb + "并不再询问" , Toast.LENGTH_SHORT).show();
//            new AppSettingsDialog
//                    .Builder(this)
//                    .setRationale("此功能需要" + sb + "权限，否则无法正常使用，是否打开设置")
//                    .setPositiveButton("好")
//                    .setNegativeButton("不行")
//                    .build()
//                    .show();
//        }

    }
}
